#include<bits/stdc++.h>
using namespace std;


bool checkId(string id) {
    if(id.size() != 18)
        return false;

    for(auto i : id)
        if(!isdigit(i))
            return false;

    return true;
}


struct info {
    string name, id;
    int state;
    string submitTime;
} a[1500];


unordered_map<string, int> mp;

set<string> st;
vector<info> output;

signed main() {

    int D, P;
    cin >> D >> P;

    for(int d = 1; d <= D; d++) {
        int T, S;
        cin >> T >> S;

        string name, id, submitTime;
        int state, num = 0;
        while(T--) {
            cin >> name >> id >> state >> submitTime;

            if(!checkId(id)) continue;

            a[++num] = {name, id, state, submitTime};

            if(state && !st.count(id)) {
                st.insert(id);
                output.push_back(a[num]);
            }
        }

        stable_sort(a + 1, a + num + 1, [](info x, info y) {
            return x.submitTime < y.submitTime;
        });

        for(int i = 1; i <= num; i++) {
            if(S <= 0)	break;

            if(!mp.count(a[i].id) || mp[a[i].id] + P < d) {
                mp[a[i].id] = d;
                cout << a[i].name << " " << a[i].id << "\n";
                S--;
            }
        }
    }

    for(auto op : output)
        cout << op.name << " " << op.id << "\n";

    return 0;
}

